package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.common.ApiSignUtil;
import com.robotserver.orders.common.SignPojo;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.entity.TbOrders;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.param.PddOldOrderSyncParam;
import com.robotserver.orders.param.PddOrderSyncParam;
import com.robotserver.orders.service.PddOrderService;
import com.robotserver.orders.service.TbOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.TreeMap;

/**
 * @Auther: jianghan
 * @Date: 2019/11/28 11:44
 * @Description:
 */
@RestController
public class ApiController {

    private static Logger logger = LoggerFactory.getLogger(ApiController.class);
    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private PddOrderService pddOrderService;

    @Value("${sign.key}")
    private String signkey;

    /**
     * 失效订单上报
     * @param orders
     * @return
     */
    @PostMapping("tborder/invalid/push")
    public Object invalidPush(String orders){
        if(StringUtils.isBlank(orders)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }
        List<TbOrders> tbOrders = JSONObject.parseArray(orders,TbOrders.class);
        tbOrderService.invalid(tbOrders);
        return true;
    }

    /**
     * 拼多多订单拉取
     * @param pddOrderSyncParam
     * @return
     */
    @PostMapping("pddorder/remotePull")
    public Object remotePull(PddOrderSyncParam pddOrderSyncParam){
        pddOrderService.syncOrder(pddOrderSyncParam);
        return true;
    }


    /**
     * 拼多多订单拉取支持老软件
     * @param pddOldOrderSyncParam
     * @return
     */
    @PostMapping("pddorder/oldPull")
    public Object oldPull(PddOldOrderSyncParam pddOldOrderSyncParam, HttpServletRequest request){
        SignPojo signPojo = getParams(request);
        String sign = ApiSignUtil.getSign(signPojo.getTreeMap(),signkey);

        if (!sign.equals(signPojo.getSign())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.SIGN_ERR);
        }
        return pddOrderService.oldSyncOrder(pddOldOrderSyncParam);
    }

    private SignPojo getParams(HttpServletRequest request){
        SignPojo signPojo = new SignPojo();
        TreeMap<String,Object> treeMap = new TreeMap<>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
//				if (StringUtils.isNotBlank(paramValue)) {
                if ("sign".equals(paramName)) {
                    signPojo.setSign(paramValue);
                }else {
                    if(!"t".equalsIgnoreCase(paramName)) {
                        treeMap.put(paramName.toLowerCase(), paramValue);
                    }
                }
//				}
            }
        }
        signPojo.setTreeMap(treeMap);
        return signPojo;
    }
}
